package com.dd.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.DdSystemuser;
import com.dd.entity.DdUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * DdSystemuser entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.dd.entity.DdSystemuser
 * @author MyEclipse Persistence Tools
 */

public class DdSystemuserDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdSystemuserDAO.class);
	// property constants
	public static final String LOGID = "logid";
	public static final String PASSWORD = "password";
	public static final String TYPE = "type";
	public static final String NAME = "name";

	protected void initDao() {
		// do nothing
	}

	public void save(DdSystemuser transientInstance) {
		log.debug("saving DdSystemuser instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DdSystemuser persistentInstance) {
		log.debug("deleting DdSystemuser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DdSystemuser findById(java.lang.Integer id) {
		log.debug("getting DdSystemuser instance with id: " + id);
		try {
			DdSystemuser instance = (DdSystemuser) getHibernateTemplate().get(
					"com.dd.entity.DdSystemuser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(DdSystemuser instance) {
		log.debug("finding DdSystemuser instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding DdSystemuser instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from DdSystemuser as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public DdSystemuser findByShopid(Object shopid) {
		try {
			String queryString = "from DdSystemuser as model where model.shopid= ?";
			List li = getHibernateTemplate().find(queryString, shopid);
			if (li.size() != 0) {
				return (DdSystemuser) li.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find by property name failed", re);
			throw re;

		}
	}

	public DdSystemuser findByLogid(Object logid) {
		try {
			String queryString = "from DdSystemuser as model where model.logid= ?";
			List li = getHibernateTemplate().find(queryString, logid);
			if (li.size() != 0) {
				return (DdSystemuser) li.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find by property name failed", re);
			throw re;

		}
	}

	public DdSystemuser findByLogid(Object logid, String type) {
		try {
			String queryString = "from DdSystemuser as model where model.logid= ? and type in ("
					+ type + ")";
			List li = getHibernateTemplate().find(queryString, logid);
			if (li.size() != 0) {
				return (DdSystemuser) li.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find by property name failed", re);
			throw re;

		}
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public DdSystemuser findByName(String name,int type) {
		try {
			String queryString = "from DdSystemuser where name='"+name+"' and type="+type;
			List li = getHibernateTemplate().find(queryString);
			if (li.size() != 0) {
				return (DdSystemuser) li.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find by property name failed", re);
			throw re;

		}
	}

	public List findAll() {
		log.debug("finding all DdSystemuser instances");
		try {
			String queryString = "from DdSystemuser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public DdSystemuser merge(DdSystemuser detachedInstance) {
		log.debug("merging DdSystemuser instance");
		try {
			DdSystemuser result = (DdSystemuser) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(DdSystemuser instance) {
		log.debug("attaching dirty DdSystemuser instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(DdSystemuser instance) {
		log.debug("attaching clean DdSystemuser instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DdSystemuserDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (DdSystemuserDAO) ctx.getBean("DdSystemuserDAO");
	}
}